package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserOrder
//@description: 创建UserOrder记录
//@param: userOrder model.UserOrder
//@return: err error

func CreateUserOrder(userOrder model.UserOrder) (err error) {
	err = global.GVA_DB.Create(&userOrder).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrder
//@description: 删除UserOrder记录
//@param: userOrder model.UserOrder
//@return: err error

func DeleteUserOrder(userOrder model.UserOrder) (err error) {
	err = global.GVA_DB.Delete(&userOrder).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrderByIds
//@description: 批量删除UserOrder记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserOrderByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserOrder{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserOrder
//@description: 更新UserOrder记录
//@param: userOrder *model.UserOrder
//@return: err error

func UpdateUserOrder(userOrder model.UserOrder) (err error) {
	err = global.GVA_DB.Save(&userOrder).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrder
//@description: 根据id获取UserOrder记录
//@param: id uint
//@return: err error, userOrder model.UserOrder

func GetUserOrder(id uint) (err error, userOrder model.UserOrder) {
	err = global.GVA_DB.Where("id = ?", id).Preload(`UserOrderPay.SysUser`).Preload(`Project`).Preload(`UserOrderPay`).Preload(`BuyUsers`).Preload(`SellerUser`).Preload(`UserOrderDetail`).Preload(`UserOrderGetPic`).Preload(`UserOrderSendPic`).Preload(`UserOrderDetail.CWorkInfo`).Preload(`UserOrderDetail.CProductInfo`).First(&userOrder).Error
	return
}


func GetOrderUserQy(order string)(err error, userOrders []model.UserOrder){
	err = global.GVA_DB.Preload(`BuyUsers`).Where(`status>=3`).Group(`buy_id`).Preload(`UserOrderDetail`).Preload(`UserOrderDetail.UserOrder`).Order(order).Find(&userOrders).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrderInfoList
//@description: 分页获取UserOrder记录
//@param: info request.UserOrderSearch
//@return: err error, list interface{}, total int64

func GetUserOrderInfoList(info request.UserOrderSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserOrder{})
    var userOrders []model.UserOrder
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.BuyId != 0 {
        db = db.Where("`buy_id` = ?",info.BuyId)
    }
    if info.IsPublic != nil {
        db = db.Where("`is_public` = ?",info.IsPublic)
    }
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
    if info.OrderSn != "" {
        db = db.Where("`order_sn` = ?",info.OrderSn)
    }
    if info.ProjectId != 0 {
        db = db.Where("`project_id` = ?",info.ProjectId)
    }
    if info.SellerId != 0 {
        db = db.Where("`seller_id` = ?",info.SellerId)
    }
    if info.Status != 0 {
        db = db.Where("`status` = ?",info.Status)
    }
    if info.UserSn != "" {
        db = db.Where("`user_sn` = ?",info.UserSn)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`UserOrderPay.SysUser`).Preload(`Project`).Preload(`UserOrderPay`).Preload(`BuyUsers`).Preload(`SellerUser`).Preload(`UserOrderDetail`).Preload(`UserOrderGetPic`).Preload(`UserOrderSendPic`).Find(&userOrders).Error
	return err, userOrders, total
}